<?php
/*
 * DBConnector2 - PHP-MySQL simple connector and getter methods.
 * Copyright (c) 2009-2011 Simone Lusenti <lusenti.s@gmail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
 
require_once 'Query.class.php';

class SQLException extends Exception {}

class DBConnector2 {

	private $host='localhost';
	private $user='root';
	private $password='toor';
	private $db_name='test';
	
	private $handle = NULL;
	private $filter = NULL;
	
	function DBConnector2($db_name='') {
		if (!empty($db_name))
			$this->db_name=$db_name;
	}
	
	static function dbError(array $info) {
		throw new SQLException("[{$info['function']}]: {$info['error']}");
	}
	
	static function join($table1, $col1, $table2, $col2, $join_type='INNER') {
		return "`$table1` $join_type JOIN `$table2` ON `$table1`.`$col1`=`$table2`.`$col2`";
	}

	function connect() {
		$this->handle = mysql_pconnect($this->host, $this->user, $this->password);
		if ($this->handle==FALSE && isset($_SERVER['REQUEST_URI'])) {
			DBConnector2::dbError("dbconnect: " . mysql_error());
		}
		mysql_select_db($this->db_name) or die('error: ' . mysql_error());
		return $this->handle != null;
	}

	function executeQuery(Query $sql) {
		$res = @mysql_query($sql->get());
		if ($res === false){ DBConnector2::dbError(array('function'=>"Query",'error'=>mysql_error())); }
		return $res;
	}
	
	function getFirstRow(Query $sql) {
		$res = $this->executeQuery($sql);
		$a_res = $this->fetch($res);
		return $a_res;
	}
	
	function getField(Query $sql, $fieldName) {
		$res = $this->getFirstRow($sql);
		return $res[$fieldName];
	}
	
	function hasRows(Query $sql) {
		$res = $this->executeQuery($sql);
		return $this->numRows($res);
	}
	
	function getArray(Query $sql) {
		$res = $this->executeQuery($sql);
		return $this->resToArray($res);
	}
	
	function getArrayGroupID(Query $sql, $colname) {
		$arr = array();
		$res = $this->executeQuery($sql);
		while ($row = $this->fetch($res)) {
			$arr[$row[$colname]] = $row;
		}
		return $arr;
	}

	function getArrayGroup(Query $sql, $colname) {
		$arr = array();
		$res = $this->executeQuery($sql);
		while ($row = $this->fetch($res)) {
			$arr[$row[$colname]][] = $row;
		}
		return $arr;
	}

	function executeUpdate(Query $sql) {
		$res = @mysql_query($sql->get());
		if ($res === false){ DBConnector2::dbError(array('function'=>"Update",'error'=>mysql_error())); }
		return @mysql_affected_rows();
	}
	
	function lastID() {	
		return mysql_insert_id();
	}

	function fetch($res) {
		return mysql_fetch_assoc($res);
	}
	
	function numRows($res) {
		return mysql_num_rows($res);
	}
	
	function resToArray($res) {
		$arr = array();
		while ($row = $this->fetch($res)) {
			$arr[] = $row;
		}
		return $arr;		
	}
	
	function describeTable($tblname) {
		$res = $this->executeQuery(new Query('DESCRIBE %', array($tblname), '`'));
		return $this->resToArray($res);
	}

}
?>
